<?php 
class ShopAttribute extends Object
{	
	function getShopAttributes($id=0)
	{
		if($id > 0)
		{ $select = 'SELECT * FROM '.DB_PERFIX.'product_attributes WHERE id='.$id.';'; }
		else
		{ $select = 'SELECT * FROM '.DB_PERFIX.'product_attributes;'; }
		$result = mysql_query($select);
		if(mysql_num_rows($result) > 0)
		{
			if($id > 0)
			{
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$key] = $row[$key]; }
				}
			}
			else
			{
				$i=0;
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$i][$key] = $row[$key]; }
					$i++;
				}
			}
			return $item;
		}
	}
	
	function getShopAttributesMenuHTML($attributes)
	{
		if($attributes)
		{
			$output .= '<ul>';
			foreach($attributes as $attribute)
			{

				$style = ' style="background-image:url('.SYSICONS_URL.'treemenu/object_16.gif)" ';
				$output .= '<li id="atr_'.$attribute['id'].'" class="'.$li_class.' leaf" '.$state.' rel="product_atr">';
				$output .= '<a '.$style.' href="?con='.$_REQUEST['con'].'&amp;attribute='.$attribute['id'].'">';
				$output .= $attribute['title'];
				$output .= '</a>';
				$output .= '</li>';
			}
			$output .= '</ul>';
		}
		return $output;
	}
	
	function addShopAttribute($title, $title_import='')
	{
		if($title != '')
		{
			$insert  = 'INSERT INTO '.DB_PERFIX.'product_attributes (`title`, `title_import`)'; 
			$insert .= 'VALUES ("'.$title.'", "'.$title_import.'");';
			$result = mysql_query($insert);
			if ($result > 0)
			{
				$result = mysql_query("SELECT id FROM ".DB_PERFIX."product_attributes ORDER BY id DESC LIMIT 1;");
				while($row = mysql_fetch_array($result))
				{ $item['id'] = $row['id']; }
				
				if($item['id'] > 0)
				{ $item = self::getShopAttributes($item['id']); }
			}
			return $item;
		}
	}
	
	function findCreateAttribute(&$attributes, $find)
	{
		$result = 0;
		foreach($attributes as $attribute)
		{
			if($attribute['import_title'] == $find)
			{ $result = $attribute['id'];  }
		}
		if($result == 0)
		{
			foreach($attributes as $attribute)
			{
				if(strtolower($attribute['title']) == strtolower($find))
				{ $result = $attribute['id'];  }
			}
		}
		if($result == 0)
		{
			$attributes[(count($attributes)+1)] = self::addShopAttribute($find, strtolower($find));
			$result = $attributes[(count($attributes))]['id'];
		}
		return $result;
	}
	
	function saveShopAttribute($id, $title, $title_import='', $type=0, $effect=0, $predefined=0, $display=0, $display_list=0, $val_types=0)
	{	
		if($id > 0)
		{
			$update  = 'UPDATE '.DB_PERFIX.'product_attributes SET ';
			$update .= 'title="'.$title.'", ';
			$update .= 'title_import="'.$title_import.'", ';
			$update .= 'type='.$type.', ';
			$update .= 'predefined='.$predefined.', ';
			$update .= 'display='.$display.', ';
			$update .= 'display_list='.$display_list.', ';
			$update .= 'val_types='.$val_types.', ';
			$update .= 'effect='.$effect.' ';
			$update .= 'WHERE id='.$id.' LIMIT 1;';
			$update = mysql_query($update);
		}
	}
	
	function getShopAttributeValues($id=0)
	{
		if($id > 0)
		{
			$select = 'SELECT * FROM '.DB_PERFIX.'product_attribute_values WHERE attribute_id='.$id.';';
			$result = mysql_query($select);
			if(mysql_num_rows($result) > 0)
			{
				$i=0;
				while($row = mysql_fetch_array($result))
				{
					foreach(array_keys($row) as $key)
					{ $item[$i][$key] = $row[$key]; }
					$i++;
				}
				return $item;
			}
		}
	}
	
	function remShopAttributeValue($id, $del_existing=0)
	{
		if($id > 0)
		{
			if($del_existing == 1)
			{
				$update = 'DELETE FROM '.DB_PERFIX.'product_vals WHERE value_id='.$id.';';
				$result = mysql_query($update);
				$update = 'DELETE FROM '.DB_PERFIX.'product_var_vals WHERE value_id='.$id.';';
				$result = mysql_query($update);
				$update = 'DELETE FROM '.DB_PERFIX.'product_vals_big WHERE value_id='.$id.';';
				$result = mysql_query($update);
			}
			else
			{
				$update = 'UPDATE '.DB_PERFIX.'product_vals SET value_id=0 WHERE value_id='.$id.';';
				$result = mysql_query($update);
				$update = 'UPDATE '.DB_PERFIX.'product_var_vals SET value_id=0 WHERE value_id='.$id.';';
				$result = mysql_query($update);
				$update = 'UPDATE '.DB_PERFIX.'product_vals_big SET value_id=0 WHERE value_id='.$id.';';
				$result = mysql_query($update);
			}
			
			$delete = 'DELETE FROM '.DB_PERFIX.'product_attribute_values WHERE id='.$id.' LIMIT 1;';
			$result = mysql_query($delete);
			return ;
		}
	}
}

?>